/*
 * @Author: szx
 * @Date: 2021-11-28 13:09:06
 * @LastEditTime: 2021-11-28 13:26:26
 * @Description:
 * @FilePath: \leetcode\400-499\438\438.js
 */
/**
 * @param {string} s
 * @param {string} p
 * @return {number[]}
 */
var findAnagrams = function (s, p) {
    const m = s.length,
        n = p.length;
    const res = [];
    if (m < n) {
        return res;
    }
    const arrp = new Array(26).fill(0);
    for (let ch of p) {
        arrp[ch.charCodeAt() - 'a'.charCodeAt()]++;
    }

    const arrs = new Array(26).fill(0);
    for (let i = 0; i < n - 1; i++) {
        arrs[s[i].charCodeAt() - 'a'.charCodeAt()]++;
    }
    for (let i = n - 1; i < m; i++) {
        arrs[s[i].charCodeAt() - 'a'.charCodeAt()]++;
        if (arrs.toString() == arrp.toString()) res.push(i - n + 1);
        arrs[s[i - n + 1].charCodeAt() - 'a'.charCodeAt()]--;
    }
    return res;
};
